home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / SmartReadArgs.lha / smartreadargs / test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-04  |  3.6 KB  |  174 lines

  1. /*
  2.  * test.c -- Test SmartReadArgs()
  3.  *
  4.  * $VER: test.c 1.1 (1.9.98)
  5.  *
  6.  * Copyright 1998 by Thomas Aglassinger <agi@sbox.tu-graz.ac.at>
  7.  *
  8.  * Based on ExtReadArgs Copyright 1994,1995 by Stefan Ruppert
  9.  */
  10.  
  11. #include "debug.h"
  12. #include "SmartReadArgs.h"
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <exec/libraries.h>
  17. #include <utility/tagitem.h>
  18. #include <dos/exall.h>
  19. #include <intuition/intuition.h>
  20.  
  21. #include <proto/exec.h>
  22. #include <proto/dos.h>
  23. #include <proto/icon.h>
  24. #include <proto/intuition.h>
  25.  
  26. #include <clib/alib_stdio_protos.h>
  27.  
  28. /* ReadArgs()-template and indices to address parameter array */
  29. #define TEMPLATE     "FILES/M/A,VERBOSE/S"
  30.  
  31. enum
  32. {
  33.    ARG_FILES,
  34.    ARG_VERBOSE,
  35.    ARG_MAX
  36. };
  37.  
  38. /* Description of filetypes */
  39. STRPTR filetypes[] =
  40. {
  41.    "pipe",
  42.    "linkfile",
  43.    "file",
  44.    NULL,
  45.    NULL,
  46.    NULL,
  47.    "root",
  48.    "dir",
  49.    "softlink",
  50.    "linkdir",
  51.    NULL};
  52.  
  53. #ifdef __GNUC__
  54. /* Contains WBStartup message if gcc/libnix is used */
  55. extern struct WBStartup *_WBenchMsg;
  56.  
  57. /* Libnix output window */
  58. char __stdiowin[] = "con:///200/libnix stdio/AUTO/CLOSE/WAIT";
  59. #endif
  60.  
  61. /*
  62.  * Main function
  63.  */
  64. int main(int argc, STRPTR argv[])
  65. {
  66.    int return_code = RETURN_ERROR;
  67.  
  68.    struct WBStartup *wbstart = NULL;
  69.    struct SmartArgs smart_args =
  70.    {NULL};
  71.    LONG argument[ARG_MAX] =
  72.    {NULL};
  73.  
  74.    LONG error;
  75.  
  76.    D(bug("argc = %d\n", argc));
  77.  
  78.    /* Get WBStartup message */
  79. #ifdef __GNUC__
  80.    wbstart = _WBenchMsg;
  81. #else
  82.    if (argc == 0)
  83.    {
  84.       wbstart = (struct WBStartup *) argv;
  85.    }
  86. #endif
  87.  
  88.    /* Prepare SmartArgs structure */
  89.    smart_args.sa_Template = TEMPLATE;
  90.    smart_args.sa_Parameter = argument;
  91.    smart_args.sa_FileParameter = ARG_FILES;
  92.    smart_args.sa_Window = "CON://500/200/Test SmartReadArgs from Workbench/AUTO/WAIT/CLOSE";
  93.  
  94.    /* Attempt to parse arguments */
  95.    error = SmartReadArgs(wbstart, &smart_args);
  96.  
  97.    /* If parsing was successful, display information about all files passed */
  98.    if (error == 0)
  99.    {
  100.       struct FileInfoBlock *fib = AllocDosObject(DOS_FIB, NULL);
  101.  
  102.       D(bug("started from workbench : %ld\n",
  103.             smart_args.sa_Flags & SAF_WORKBENCH));
  104.  
  105.       if (fib != NULL)
  106.       {
  107.          STRPTR *files = (STRPTR *) argument[ARG_FILES];
  108.          BPTR lock;
  109.  
  110.          while (*files)
  111.          {
  112.             D(bug("examine \"%s\"\n", *files));
  113.             if ((lock = Lock(*files, SHARED_LOCK)))
  114.             {
  115.                if (Examine(lock, fib))
  116.                {
  117.                   printf("%-32s ", fib->fib_FileName);
  118.  
  119.                   if (argument[ARG_VERBOSE])
  120.                      printf("%8ld (%s)", fib->fib_Size, filetypes[fib->fib_DirEntryType + 5]);
  121.  
  122.                   puts("");
  123.                }
  124.                UnLock(lock);
  125.             }
  126.             files++;
  127.          }
  128.  
  129.          FreeDosObject(DOS_FIB, fib);
  130.       }
  131.  
  132.       error = IoErr();
  133.    }
  134.  
  135.    /* Free resources allocated by SmartReadArgs(), even in case of error
  136.     * during parsing */
  137.    SmartFreeArgs(&smart_args);
  138.  
  139.    /* Do the error handling */
  140.    if (error)
  141.    {
  142.       if (smart_args.sa_Flags & SAF_WORKBENCH)
  143.       {
  144.          /* Display error in requester if started from Workbench */
  145.          UBYTE buffer[100];
  146.          struct EasyStruct es =
  147.          {
  148.             sizeof(struct EasyStruct),
  149.             0,
  150.             "Test",
  151.             "%s",
  152.             "Cancel"
  153.          };
  154.  
  155.          Fault(error, "Error", buffer, sizeof(buffer));
  156.  
  157.          EasyRequest(NULL, &es, NULL, buffer);
  158.       }
  159.       else
  160.       {
  161.          /* Display error in console if started from CLI */
  162.          PrintFault(error, "Test");
  163.       }
  164.    }
  165.  
  166.    /* Update the return code */
  167.    if (error == 0)
  168.    {
  169.       return_code = RETURN_OK;
  170.    }
  171.  
  172.    return return_code;
  173. }
  174.